home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / MacPerl 506 appl folder.sit / MacPerl 506 appl folder / Mac_Perl_506r1m_appl / lib / newfind.pl < prev    next >
Text File  |  1993-10-23  |  2KB  |  89 lines

  1. # Usage:
  2. #    require "find.pl";
  3. #
  4. #    &find('/foo','/bar');
  5. #
  6. #    sub wanted { ... }
  7. #        where wanted does whatever you want.  $dir contains the
  8. #        current directory name, and $_ the current filename within
  9. #        that directory.  $name contains "$dir/$_".  You are cd'ed
  10. #        to $dir when the function is called.  The function may
  11. #        set $prune to prune the tree.
  12. #
  13. # This library is primarily for find2perl, which, when fed
  14. #
  15. #   find2perl / -name .nfs¥* -mtime +7 -exec rm -f {} ¥; -o -fstype nfs -prune
  16. #
  17. # spits out something like this
  18. #
  19. #    sub wanted {
  20. #        /^¥.nfs.*$/ &&
  21. #        (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  22. #        int(-M _) > 7 &&
  23. #        unlink($_)
  24. #        ||
  25. #        ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
  26. #        $dev < 0 &&
  27. #        ($prune = 1);
  28. #    }
  29. #
  30. # Converted to Mac by Charlie Reiman <reiman@kaleida.com>
  31. #
  32.  
  33. sub find {
  34.     chop($cwd = `pwd`);
  35.     foreach $topdir (@_) {
  36.     if (-d $topdir) {
  37.         if (chdir($topdir)) {
  38.         ($dir,$_) = ($topdir,':');
  39.         $name = $topdir;
  40.         &wanted;
  41.         $topdir =~ s,:$,, ;
  42.         &finddir($topdir);
  43.         }
  44.         else {
  45.         warn "Can't cd to $topdir: $!¥n";
  46.         }
  47.     }
  48.     else {
  49.         unless (($dir,$_) = $topdir =~ m#^(.*:)(.*)$#) {
  50.         ($dir,$_) = (':', $topdir);
  51.         }
  52.         $name = $topdir;
  53.         chdir $dir && &wanted;
  54.     }
  55.     chdir $cwd;
  56.     }
  57. }
  58.  
  59. sub finddir {
  60.     local($dir) = @_;
  61.     local($nlink);
  62.     local($name);
  63.  
  64.     # Get the list of files in the current directory.
  65.  
  66.     opendir(DIR,':') || (warn "Can't open $dir: $!¥n", return);
  67.     local(@filenames) = readdir(DIR);
  68.     closedir(DIR);
  69.  
  70.     for (@filenames) {
  71.         $nlink = $prune = 0;
  72.         $name = "$dir:$_";
  73.         &wanted;
  74.  
  75.     # Get link count and check for directoriness.
  76.  
  77.     if (-d $_) {
  78.  
  79.         # It really is a directory, so do it recursively.
  80.  
  81.         if (!$prune && chdir $_) {
  82.          &finddir($name);
  83.         chdir '::';
  84.         }
  85.         }
  86.     }
  87. }
  88. 1;
  89.